import { MessageInfo, OutputStatus, PluginType } from './type';
import { isObject, isArray } from '@material/helpers';

export const isEndChuns = function (info: MessageInfo) {
    if (
        isObject(info.Content) &&
        isObject(info.Content.chunk) &&
        isArray(info.Content.chunk.choices) &&
        info.Content.chunk.choices.length
    ) {
        const { finish_reason: finishReason } = info.Content.chunk.choices[0];
        if (finishReason) {
            return finishReason;
        }
        return '';
    }
};

export const getMessageContent = function (info: any) {
    if (
        isObject(info.Content) &&
        isObject(info.Content.chunk) &&
        isArray(info.Content.chunk.choices) &&
        info.Content.chunk.choices.length
    ) {
        const { delta } = info.Content.chunk.choices[0];
        if (delta?.content) {
            return delta.content;
        }
        return '';
    }
    return info.Content;
};

export const transformMessage = function (output: MessageInfo, plugins: PluginType[]) {
    return plugins.reduce((current: void | MessageInfo | MessageInfo[], plugin) => {
        if (Array.isArray(current)) {
            const results = current.flatMap((item) => {
                const result = plugin(item);
                if (!result) {
                    return [];
                }
                return Array.isArray(result) ? result : [result];
            });
            return results.length === 1 ? results[0] : results;
        }
        if (!current) {
            return [];
        }

        // 如果是单个对象，直接应用插件
        return plugin(current);
    }, output);
};

export const updateChatItemInfo = function (msg: MessageInfo, pre: MessageInfo[]) {
    if (msg.FromType !== 'user' && msg.From === 'ai' && msg.chatStatus === 'ai') {
        const list = [...pre];
        if (
            msg &&
            msg.outputStatus &&
            [OutputStatus.OutputLoading].includes(msg.outputStatus as OutputStatus)
        ) {
            return [msg, ...list];
        }

        const firtstItem = list.shift();
        if (!firtstItem) {
            return pre;
        }
        if (
            firtstItem &&
            msg.outputStatus === OutputStatus.OutputEnd &&
            firtstItem.messgeId !== msg.messgeId
        ) {
            return [msg, ...pre];
        }
        return [msg, ...list];
    }
};

export const addRenderState = function (msg: MessageInfo, pre: MessageInfo[]) {
    if (!isArray(pre) || pre.length === 0) {
        return [msg];
    }
    if (msg.FromType === 'user' && !msg.Event) {
        if (!msg.MsgId) {
            return [msg, ...pre];
        }
        const list = [...pre];
        const firtstItem = list.shift();
        if (!firtstItem) {
            return pre;
        }
        if (firtstItem.FromType === 'user' && msg.MsgId) {
            return [msg, ...list];
        }
    }
    const aiList = updateChatItemInfo(msg, pre);
    if (aiList) {
        return aiList;
    }
    return [msg, ...pre];
};
